local ListViewExtend = import(".ListViewExtend")

local fileName = string.getModeName(...)
local ListFullSpreadMenu = class( fileName )

-- params
-- list 		 列表容器，不能为空
-- btnClass 	 一级按钮类名，不能为空
-- subBtnClass   二级按钮类名，可以为空
-- panelRes 	 二级按钮容器csb，不能为空
-- operate 		 操作回调
function ListFullSpreadMenu:ctor( params )
	self._list = params.list
	self._btnClass = params.btnClass
	self._subBtnClass = params.subBtnClass
	self._operate = params.operate
	self._mutexMenu =  params.mutexMenu

	assert(self._list, "ListFullSpreadMenu.new list can't be nil")
	assert(self._btnClass and self._btnClass ~= "", "ListFullSpreadMenu.new btnClass can't be nil")

	self._list:removeAllItems()
	ListViewExtend.scrollToItemExtend(self._list)

	self._datas = nil -- 数据源

	self._btnList = {} -- 所有一级菜单列表

	self._subPanelOpenStatusList = { 1 } -- 所有二级菜单的展开状态，默认第一个二级菜单展开
	self._subPanelList = {} -- 所有二级菜单列表
	self._subPanelSizeList = {} -- 所有二级菜单size
	self._isBubPanelHadOpenedList = { 1 } -- 二级菜单是否展开过
end

function ListFullSpreadMenu:refreshList( datas )
	if self._datas == datas then return end

	for i = 1, #datas do
		self._subPanelOpenStatusList[i] = self._subPanelOpenStatusList[i] or 0 -- 保留原状态或者默认未展开
		self._isBubPanelHadOpenedList[i] = self._isBubPanelHadOpenedList[i] or 0 -- 默认是0 --没展开过
	end

	for i,data in ipairs(datas) do
		local btn = self:_createBtn(i)
		btn:setData(data)

		for i2,subData in ipairs( data.subDatas ) do -- 下辖的二级菜单
			local subbtn = self:_createSubBtn( i, i2, #data.subDatas )
			subbtn:setData(subData)
		end
	end

	self:_refreshPanel()
end

function ListFullSpreadMenu:_refreshPanel()
	for i,v in ipairs( self._subPanelList ) do
		if 0 == self._subPanelOpenStatusList[i] then -- 折叠
			v:setContentSize( cc.size( self._subPanelSizeList[i].width, 0 ) )
			v:setVisible( false )
		elseif 1 == self._subPanelOpenStatusList[i] then --展开
			local panelHeight = v:getChildrenCount() * ( self._subPanelSizeList[i].height )
			v:setContentSize( cc.size( self._subPanelSizeList[i].width, panelHeight ) )
			v:setVisible( true )
		else
			print( " ------------->>>>>>>>>> 二级菜单的展开状态未知 " )
		end
	end
end

function ListFullSpreadMenu:_createBtn(idx) -- 创建一级菜单
	local btn = self._btnList[idx]
	if not btn then
		btn = self._btnClass.create()
		btn:setClickHandler(function(sender)
			self:_btnClick(sender, idx)
		end)
		self._btnList[idx] = btn
		self._list:pushBackCustomItem(btn)
	end
	if 0 == self._subPanelOpenStatusList[idx] then
		btn:setSelected(false)
	elseif 1 == self._subPanelOpenStatusList[idx] then
		btn:setSelected(true)
	else
		print( " ----------->>>>>>>> ListFullSpreadMenu:_createBtn idx: "..idx .. " ;未知展开状态" )
	end

	return btn
end

function ListFullSpreadMenu:_createSubBtn( idx, idx2, subBtnSum ) -- 创建二级菜单
	if not self._subPanelList[idx] then -- 为空
		local panel = ccui.Layout:create()
		local subBtnTemp = self._subBtnClass.create()

		subBtnTemp:setClickHandler(function(sender)
			self:_subBtnClick(sender, idx, idx2)
		end)

		subBtnTemp:setTag( idx2 )
		local sizeTemp = subBtnTemp:getContentSize()
		subBtnTemp:setPosition( cc.p( 0, ( subBtnSum - idx2 ) * sizeTemp.height ) )
		panel:addChild( subBtnTemp )

		self._subPanelList[idx] = panel
		self._subPanelSizeList[idx] = sizeTemp
		
		self._list:pushBackCustomItem( panel )
		subBtnTemp:setParentRender(self._btnList[idx])
		return subBtnTemp
	else
		local subBtnTemp = self._subPanelList[idx]:getChildByTag( idx2 )
		if not subBtnTemp then -- 不存在的二级按钮
			subBtnTemp = self._subBtnClass.create()

			subBtnTemp:setClickHandler(function(sender)
				self:_subBtnClick(sender, idx, idx2)
			end)

			subBtnTemp:setTag( idx2 )
			local sizeTemp = subBtnTemp:getContentSize()
			subBtnTemp:setPosition( cc.p( 0, ( subBtnSum - idx2 ) * sizeTemp.height ) )

			self._subPanelList[idx]:addChild( subBtnTemp )
			self._subPanelSizeList[idx] = sizeTemp
			subBtnTemp:setParentRender(self._btnList[idx])
		end
		return subBtnTemp
	end
end


function ListFullSpreadMenu:_btnClick( btn, idx, ignoreOp )
	local data = btn:getData()
	local op = ""
	if btn:isSelected() then -- 展开
		if self._mutexMenu then
			for k, v in pairs(self._btnList) do
				if k ~= idx then
					v:setSelected(false)
					self._subPanelOpenStatusList[ k ] = 0
				end
			end
		end
		self._subPanelOpenStatusList[ idx ] = 1
		op = "open"
		self._isBubPanelHadOpenedList[ idx ] = 1
	else
		self._subPanelOpenStatusList[ idx ] = 0
		op = "close"
	end

	if not ignoreOp then
		executeFunc(self._operate, data, "btn", op, self._isBubPanelHadOpenedList[ idx ] )
	end

	self:_refreshPanel()
	self._list:getInnerContainer():requestDoLayout()
	self._list:refreshView()
	self._list:performWithDelay(function()
			if cc.isAlive(btn) then
				self._list:scrollToItem(btn, 0.8)
			end
		end, 0)
end

function ListFullSpreadMenu:_subBtnClick( subBtn, idx, idx2, ignoreOp )
	local data = subBtn:getData()
	if not ignoreOp then
		executeFunc(self._operate, data, "subBtn", nil, nil )
	end
end

function ListFullSpreadMenu:remove()
	-- self._list:removeAllItems()

	self._list = nil
	self._btnClass = nil
	self._subBtnClass = nil
	self._operate = nil

	self._datas = nil -- 数据源
	self._btnList = {} -- 所有一级菜单列表
	self._btnList = nil
	self._subPanelOpenStatusList = {} -- 所有二级菜单的展开状态
	self._subPanelOpenStatusList = nil
	self._subPanelList = {} -- 所有二级菜单列表
	self._subPanelList = nil

	self._isBubPanelHadOpenedList = {}
	self._isBubPanelHadOpenedList = nil
end

return ListFullSpreadMenu
